<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/老师2.png" alt="" />
      </div>
      <!-- 登陆表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
          ref="loginFormRef"
        class="login_form main"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-s-grid"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->




<el-select  v-model="value" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
  
        <el-switch
         style="float:right;padding-top:10px;"
  v-model="value1"
  active-text="记住密码"
   class="btns_switch">
</el-switch>

        <el-form-item class="btns" style="margin-top:10px;" >
          
          </el-switch>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //这是登陆表单的数据绑定对象
      loginForm: {
        username: "",
        password: "",
      },
      value1: true,

      loginFormRules: {
        //验证用户名是否合法
        username: [
          {
            required: true,
            message: "请输入正确的学号",
            trigger: "blur",
          },
          {
            min: 5,
            max: 13,
            message: "长度在5-13之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入正确的密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在6-12之间", trigger: "blur" },
        ],
      },
      options: [
        {
          value: 1,
          label: "学生",
        },
        {
          value: 2,
          label: "老师",
        },
        {
          value: 3,
          label: "管理员",
        },
      ],
      value: "",
    };
  },
  methods: {
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post("user/login", {
          username: this.loginForm.username,
          password: this.loginForm.password,
          type: this.value,
          jizhumima: this.value1,
        });
        console.log(res);
        // let data = result.data;
        if (res.code >= 0) {
          this.$message.success("登陆成功");
          window.sessionStorage.setItem("token", res.note.credentials);
          //通过编程式导航跳转到后台主页
          this.$router.push("/home");
        } else {
          return this.$message.error("用户名或密码错误，请重试！");
        }
        // .then((res) => {
        //   //console.log(res);
        //   let data = res.data;
        //   if (data.code >= 0) {
        //     alert("success!");
        //   } else {
        //     alert("error:" + data.description);
        //   }
        // });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  // background-color: #2b4b6b;
  background: url("../assets/gate.jpg") 0 / cover fixed;
  height: 100%;
}
.login_box {
  opacity: 0.85;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
// .el-select {
//   width: 50%;
//   display: inline;
//   // justify-content: end;
// }
</style>